<template>
<div>
    <Form ref="EditForm" :model="FormData" label-position="top">
        <Row :gutter="16">
            <!-- <Col :xs="24" :md="24">
            <FormItem label="用户名" prop="UserName" :rules="{required: true, message:'不能为空', trigger: 'blur'}">
                <Input v-model="FormData.UserName"></Input>
            </FormItem>
            </Col> -->


            <Col :xs="24" :md="8" v-if="State.isShowPartnersSelect">
            <FormItem label="所属合作伙伴" prop="UserPartnersID">
                <PartnersSelect v-model="FormData.UserPartnersID" ></PartnersSelect>
            </FormItem>
            </Col>

            <Col :xs="24" :md="8">
            <FormItem label="姓名" prop="UserRealName" :rules="{required: true, message:'不能为空', trigger: 'blur'}">
                <Input v-model="FormData.UserRealName"></Input>
            </FormItem>
            </Col>

            <Col :xs="24" :md="8">
            <FormItem label="工号" prop="UserJobNum" :rules="{required: false, message:'不能为空', trigger: 'blur'}">
                <Input v-model="FormData.UserJobNum"></Input>
            </FormItem>
            </Col>

            <Col :xs="24" :md="8">
            <FormItem label="手机号" prop="UserMobilePhone" :rules="{required: true, message:'不能为空', trigger: 'blur'}">
                <Input v-model="FormData.UserMobilePhone"></Input>
            </FormItem>
            </Col>

            <Col :xs="24" :md="8">
            <FormItem label="角色" prop="UserRuleArray" :rules="{required: true, type: 'array',min:1, message:'至少选择一个角色', trigger: 'change'}">
            <RuleSelect v-model="FormData.UserRuleArray" :partners-id="FormData.UserPartnersID"></RuleSelect>
            </FormItem>
            </Col>

            <Col :xs="24" :md="8">
            <FormItem label="岗位" prop="UserType">
                <PartnersDictSelect  v-model="FormData.UserType" :partners-id="FormData.UserPartnersID" label="UserType"  placeholder='请选择'></PartnersDictSelect>
            </FormItem>
            </Col>


            <Col :xs="24" :md="8">
            <FormItem label="所属部门" prop="UserOrgStructID">
                <OrgSelect  v-model="FormData.UserOrgStructID" :partners-id="FormData.UserPartnersID" :parent-id="CurUserInfo.UserOrgStructID"  placeholder='请选择'></OrgSelect>
            </FormItem>
            </Col>

           <Col :xs="24" :md="8">
            <FormItem label="所属区域" prop="UserAreaCode">
                <DictSelect :multiple="false"  v-model="FormData.UserAreaCode"  label="area"  placeholder='请选择'></DictSelect>
            </FormItem>
            </Col>

             <Col :xs="24" :md="8">
            <FormItem label="所属上级" prop="UserParentID">
                <UserSelect :multiple="false" v-model="FormData.UserParentID" :partners-id="FormData.UserPartnersID" btn-text="选择技术员"  placeholder='请选择'></UserSelect>
            </FormItem>
            </Col>



        </Row>
        <Row>
            <Col>
            <FormItem>
                <Button type="primary" @click="handleSubmit()">提交</Button>
                <Button   @click="handleReset()" style="margin-left: 8px">重置</Button>
            </FormItem>
            </Col>
        </Row>


    </Form>


</div>
</template>

<script>
import then from 'thenjs';
import moment from 'moment';
import AppFrame from '../../../libs/app-frame';

import PartnersSelect from '../../../components/partners-select';
import RuleSelect from '../../../components/rule-select';
import OrgSelect from '../../../components/org-select';
import DictSelect from '../../../components/dict-select';
import PartnersDictSelect from '../../../components/partners-dict-select';
import UserSelect from '../../../components/user-select';
export default {
    components: {
        PartnersSelect,
        RuleSelect,
        OrgSelect,
        DictSelect,
        PartnersDictSelect,
        UserSelect
    },
    data: function() {
        return {
            CurUserInfo: {},
            State: { //数据加载状态
                IsLoadingData: false,
                IsChangeData: false,
                isShowPartnersSelect:false
            },
            FormData: {
                UserPartnersID: 0,
                UserRealName:'',
                UserJobNum:'',
                UserMobilePhone:'',
                UserRuleArray:[],
                UserOrgStructID:'',
                UserAreaCode:''

            },
            OrgStruct: [],
            Query: {}
        }
    },
    created: function() {
        var self = this;
        this.CurUserInfo = AppFrame.UserInfo.getUserInfo();
        this.Query = this.$route.query;
        this.getFormData();
    },
    watch:{
        "FormData.UserPartnersID":function(curVal){
           // alert("watch" + JSON.stringify(this.FormData));
        }
    },

    methods: {
        getFormData: function() {
            let self = this;
            self.State.IsLoadingData = true;
            let postData = {
                uid: self.Query.uid || 0,
            };
            AppFrame.Http.Post("/manage-api/system/admin-user/getItem", postData, function(err, ret) {
                self.State.IsLoadingData = false;
                if (ret && ret.Result == 1) {

                    self.State.isShowPartnersSelect=ret.Data.isShowPartnersSelect;
                    self.OrgStruct = ret.Data.OrgStruct;
                    self.FormData = ret.Data.model;

                    if (self.FormData.UserRule) {
                        self.FormData.UserRuleArray = self.FormData.UserRule.split(',');
                       // alert(JSON.stringify(self.FormData.UserRuleArray));
                    } else {
                        self.FormData.UserRuleArray = [];
                    }

                } else {
                    self.$Message.error('获取数据错误');
                }
            });
        },
        handleSubmit() {
            let self = this;
            this.$refs["EditForm"].validate((valid) => {
                if (valid) {
                    self.FormData.UserRule = self.FormData.UserRuleArray.join(',');
                    AppFrame.Http.Post("/manage-api/system/admin-user/edit", self.FormData, function(err, ret) {
                        if (ret && ret.Result == 1) {
                            self.$Message.success('保存成功');
                            self.$router.go(-1);
                        } else {
                            self.$Message.error(ret.Msg);
                        }
                    })
                }
            });
        },
        handleReset() {
            this.$refs["EditForm"].resetFields();
        }

    }
}
</script>

<style scoped>

</style>

